<?
//Labels 0.0.4
class sym_labels extends symbiont{
    public function ___main($design=""){
        global $sql, $sym;
        $design=$this->_check($design, 'main');
        $d=new design($design, 'labels', false);
        $labels=$sql->query_each("SELECT lbl.*, lang.abbr FROM `labels` as lbl INNER JOIN `languages` as lang ON lbl.lng_id=lang.id WHERE lbl.name!='' ORDER BY `name`, `lng_id`");
        $this->labels='';
        $this->content='';
        $prev='';
        $prev_v=array();
        if(is_array($labels))
        foreach($labels as $key=>$val){
            if($prev==''){
                $v=explode('.', $val['name']);
                foreach($v as $va){
                    if(!$va) continue;
                    $this->title=$va;
                    $this->labels.=design::run($this->start);
                }
                
            }
            $name=$val['name'];
            if($prev!=$name&&$prev!=''){
                $v=explode('.', $val['name']);
                $c1=count($v)-1;
                $c2=count($prev_v);
                $j=0;
                for($i=0; isset($v[$i]); $i++){
                    if(isset($prev_v[$i])&&$v[$i]==$prev_v[$i]&&!$j) continue;
                    if(!$j){
                        $j=$i;
                        while($j<$c2){
                            $this->labels.=design::run($this->end);
                            $j++;
                        }
                    }
                    $this->title=$v[$i];
                    $this->labels.=design::run($this->start);
                }
            }
            $this->id=$val['id'];
            $this->value=$val['value']?$val['value']:'(empty)';
            $this->abbr=$val['abbr'];
            $this->labels.=design::run($this->label);
            $prev=$name;
            $prev_v=$v;
        }
        $c=count($prev_v);
        for($i=0; $i<$c;$i++){
            $this->labels.=design::run($this->end);
        }
        $d->inc();
    }
    public function ___add($design=''){
        global $sql;
        $design=$this->_check($design, 'add');
        $d=new design($design, 'labels', false);
        
        $this->infos='';
        $this->languages='';
        $languages=$sql->select_each('languages', 'abbr', array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->abbr=$val['abbr'];
            $this->languages.=design::run($this->language);
            $this->infos.=design::run($this->info);
        }
        $d->inc();
    }
    public function ___import($design){
        $design=$this->_check($design, 'import');
        $d=new design($design, 'user');
    }
    public function ___ajax_edit($design){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        
        $id=isset($_POST['id'])?data::word($_POST['id'], 'en'):'';
        $name=isset($_POST['name'])?data::word($_POST['name'], 'en'):'';
        $abbr=isset($_POST['abbr'])?data::word($_POST['abbr'], 'en', 2):'';
        $value=isset($_POST['value'])?data::html_remove($_POST['value']):'';
        $key=$id?$id:$name;
        if($key&&$abbr&&$value){
            $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
            $l=new label($key);
            if($value=='-') $l->del();
            else $l->edit($value, $lng_id);
        }
    }
    public function ___db_add(){
        global $sql, $user;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        
        if(!isset($_POST['name'])||!isset($_POST['values'])){ print('{"error":"Not the all parametrs have been sent."}'); return; }
        $name=data::word($_POST['name'], 'en', 0, '\.');
        $values=$_POST['values'];
        if(!is_array($values)){ print('{"error":"Sent incorrect value."}'); return; }
        foreach($values as $key=>$val){
            $abbr=data::word($key, 'en', 2);
            $value=data::html_remove($val);
            if($abbr&&$value){
                $lng_id=$sql->select_each('languages', 'id', array('abbr'=>$abbr), '', '', 1);
                $l=new label($name);
                $l->add($value, $lng_id);
            }
        }
        print('{"ok":"Label added."}');
    }
    public function ___db_import($design=''){
        global $sql;
        $name=data::file_system($_POST['name']);
        $file=@file_get_contents($name);
        if(!$file){
            print('{"error":"File is emply or not exists"}');
            return;
        }
        $json=json_decode($file);
        $lng=$sql->select_each('languages', array('id', 'abbr'));
        $lang=array();
        if(!is_array($lng)){
            print('{"error":"Your languages list is empty."}');
            return;
        }
        foreach($lng as $val){
            $lang[$val['abbr']]=$val['id'];
        }
        if(!is_object($json)){
            print('{"error":"Incorrect content."}');
            return;
        }
        foreach($json as $key=>$val){
            $l=new label($key);
            foreach($val as $k=>$v){
                if(isset($lang[$k])){
                    $l->set($v, $lang[$k]);
                }
            }
        }
        print('{"ok":"Importing successfully complited."}');
    }
}
?>